<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript">
var detailValidator;
var assignValidator;

function selectTemplate() {
	$.openDialog("#dialog",
			{height:550,
			width:800,
			minHeight:450,
			minWidth:600,
			loadPage:ctx + "/biz/template/query.do",
			title:"选择模板"},
			function(data) {
				$("#taskTemplate").data("id",data.id);
				$("#taskTemplate").val(data.name);
			});
}

function selectExecutePerson() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx + "/biz/system/user/tree.jsp",
			title:"选择勘察人员"},
			function(data) {
				$("#taskExecPerson").data("id",data.data("id"));
				$("#taskExecPerson").val(data.data("name"));
			});
}

function selectProject() {
	$.openDialog("#dialog",
			{height:550,
			width:800,
			minHeight:450,
			minWidth:600,
			loadPage:ctx + "/biz/project/query.do",
			title:"选择项目"},
			function(data) {
				$("#taskProject").data("id",data.id);
				$("#taskProject").val(data.name);
			});
}

function selectPlanPoint() {
	$.openPointSelect("#dialog",
			{
				loadPage:ctx + "/biz/task/map_select.jsp",
				title:"点击地图选择坐标"
			},
			function(data) {
				$("#taskPlanLongitude").val(data.longitude);
				$("#taskPlanLatitude").val(data.latitude);
				$("#planLocationProvice").val(data.locationProvice);
				$("#planLocationCity").val(data.locationCity);
				$("#planLocationCounty").val(data.locationCounty);
				$("#planLocationStreet").val(data.locationStreet);
				$("#planLocationNumber").val(data.locationNumber);
				$("#planLocationAddress").val(data.locationAddress);
			}
	);
}

function doDelete() {
	if (!confirm('是否删除？\n请确认是否继续操作 ！')) {
		return;
	}
	
	block("#detailForm");
	var id = $("#taskId").val();
	$.ajax({
		type: "POST",
		url: "delete.json",
		data: {"id": id},
		dataType: "json",
		success: function(data){
			unblock("#detailForm");
			if(!data.succeed){
				$.alert("删除失败，原因为：\n" + data.message);
				return;
			}
			$.info("删除成功！");
			goBack1();
		},
		error: function(message){
			unblock("#detailForm");
			$.alert("删除失败：" + message);
		}
	});
}

function getTaskData(){
	var data = {
			id: $("#taskId").val(),
			name: $("#taskName").val(),
			//code: $("#taskCode").val(),
			//status: $("#taskStatus").data("code"),
			//planLongitude: $("#taskPlanLongitude").val(),
			//planLatitude: $("#taskPlanLatitude").val(),
			//planLocationProvice: $("#planLocationProvice").val(),
			//planLocationCity: $("#planLocationCity").val(),
			//planLocationCounty: $("#planLocationCounty").val(),
			//planLocationStreet: $("#planLocationStreet").val(),
			//planLocationNumber: $("#planLocationNumber").val(),
			//planLocationAddress: $("#planLocationAddress").val(),
			//planStartDate: $("#taskPlanStartDate").val(), 
			planEndDate: $("#taskPlanEndDate").val(),
			//stationCode: $("#taskStationCode").val(),
			//stationName: $("#taskStationName").val(),
			//projectId: $("#taskProject").data("id"),
			//managerName: $("#taskManagerName").val(),
			//districtCode: $("#taskDistrict").data("code"),
			//districtName: $("#taskDistrict").val(),
			//execOrganId: $("#taskExecOrgan").data("id"),
			execPersonId: $("#taskExecPerson").data("id"),
			templateId: $("#taskTemplate").data("id"),
			projectId: $("#taskProject").data("id"),
			content: $("#taskContent").val()
		};
	return data;
}

function doSave() {
	if (!detailValidator.form()) {
		return;
	}
	
	block("#detailForm");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: getTaskData(),
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

function loadItem(){
	var id = request("id");
	if(id == undefined || id == ""){
		return;
	}
	
	block("#detailForm");
	$.ajax({
		type: "GET",
		cache: false,
		url: "detail.json?id=" + id,
		dataType: "json",
		success: loadSucceedCallback,
		error:function(message){
			unblock("#detailForm");
			$.alert(message);
		}  
	});	
}

function loadSucceedCallback(data){
	unblock("#detailForm");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}
	var status = data.data.status;
	if(status == "in_process" || status == "completed" || status == ""){
		location.href = "detail.do?id=" + data.data.id;
	}
	if(status == "assigned" || status == "draft"){
		$("#deleteBtn").show();
	}
	fillItem(data.data);
}

function fillItem(item){
	$("#taskTitle").text(" - " + item.name);
	$("#taskId").val(item.id);
	$("#taskName").val(item.name);
	//$("#taskCode").val(item.code);
	$("#taskStatus").val(getTaskStatus(item.status));
	$("#taskStatus").data("code", item.status);
	//$("#taskAssignedDate").val(item.assignedDate);
	//$("#taskProject").val(item.projectName);
	//$("#taskProject").data("id", item.projectId);
	//$("#taskManagerName").val(item.managerName);
	//$("#taskPlanStartDate").val((item.planStartDate!=null)?item.planStartDate.slice(0,10):item.planStartDate);
	$("#taskPlanEndDate").val((item.planEndDate!=null)?item.planEndDate.slice(0,10):item.planEndDate);
	//$("#taskStartDate").val(item.startDate);
	//$("#taskEndDate").val(item.endDate);
	//$("#taskPlanLongitude").val(item.planLongitude);
	//$("#taskPlanLatitude").val(item.planLatitude);
	//$("#planLocationProvice").val(data.planLocationProvice);
	//$("#planLocationCity").val(data.planLocationCity);
	//$("#planLocationCounty").val(data.planLocationCounty);
	//$("#planLocationStreet").val(data.planLocationStreet);
	//$("#planLocationNumber").val(data.planLocationNumber);
	//$("#planLocationAddress").val(data.planLocationAddress);
	//$("#taskDistrict").val(item.districtName);
	//$("#taskDistrict").data("code", item.districtCode);
	//$("#taskStationCode").val(item.stationCode);
	//$("#taskStationName").val(item.stationName);
	$("#taskExecOrgan").val(item.execOrganName);
	$("#taskExecOrgan").data("id", item.execOrganId);
	$("#taskExecPerson").val(item.execPersonName);
	$("#taskExecPerson").data("id", item.execPersonId);
	$("#taskProject").val(item.projectName);
	$("#taskProject").data("id", item.projectId);
	$("#taskTemplate").val(item.templateName);
	$("#taskTemplate").data("id", item.templateId);
	$("#taskContent").val(item.content);
	$("#taskCreator").val(item.creatorName);
	$("#taskCreator").data("id", item.creatorId);
	$("#taskCreateDateTime").val(item.createDateTime);
	
	$("#taskCreatorCg").show();
	$("#taskCreateDateTimeCg").show();
	$("#taskStatusCg").show();

	/**
	if(item.status == "draft"){
		$("#assignBtn").show();
		$("#saveBtn").show();
		$("#deleteBtn").show();
		$("#taskAssignedDateCg").hide();
	} else {
		$("#assignBtn").hide();
		$("#saveBtn").hide();
		$("#deleteBtn").hide();
		$("#taskAssignedDateCg").show();
	}
	if(item.status == "completed"){
		$("#taskStartDateCg").show();
		$("#taskEndDateCg").show();
	}
	*/
	
}

$(document).ready(function() {
	//$('#taskPlanStartDate').datepicker();
	$('#taskPlanEndDate').datepicker();
	$("textarea").tah({
		moreSpace:10
	});
	$().maxlength();
	
	loadItem();
	
	detailValidator = $("#detailForm").validate({
		rules: {
			taskName:{required:true, maxlength:50 },
			//taskCode:{required:true},
			//taskProject:{required:true},
			//taskPlanStartDate:{required:true, dateISO:true},
			taskPlanEndDate:{required:true, dateISO:true},
			//taskPlanLongitude:{required:true, number:true},
			//taskPlanLatitude:{required:true, number:true},
			//taskExecOrgan:{required:false},
			taskContent:{required:false, maxlength:1000},
			taskProject:{required:true},
			taskTemplate:{required:true},
			taskExecPerson:{required:true}
		},
		messages:{
			taskName:{required:"请输入任务名称"},
			//taskCode:{required:"请输入任务编码"},
			//taskProject:{required:"请选择项目"},
			//taskPlanStartDate:{required:"请输入计划开始日期", dateISO:"请输入有效日期，如2012-12-21"},
			taskPlanEndDate:{required:"请输入要求完成日期", dateISO:"请输入有效日期，如2012-12-21"},
			//taskPlanLongitude:{required:"请选择坐标", number:"请选择坐标"},
			//taskPlanLatitude:{required:"请选择坐标", number:"请选择坐标"},
			//taskExecOrgan:{required:"请选择协作单位"},
			taskContent:{required:"请输入任务描述", maxlength:"任务描述不能超过1000个字符"},
			taskProject:{required:"请选择项目"},
			taskTemplate:{required:"请选择模板"},
			taskExecPerson:{required:"请选择勘察人员"}
		}
	});

});
</script>

<form id="detailForm" class="form-horizontal">
<input type="hidden" id="taskId" name="taskId" />
<!--  
<input type="hidden" id="planLocationProvice" name="planLocationProvice" />
<input type="hidden" id="planLocationCity" name="planLocationCity" />
<input type="hidden" id="planLocationCounty" name="planLocationCounty" />
<input type="hidden" id="planLocationStreet" name="planLocationStreet" />
<input type="hidden" id="planLocationNumber" name="planLocationNumber" />
<input type="hidden" id="planLocationAddress" name="planLocationAddress" />
-->
<fieldset>
	<div class="control-group">
		<label for="taskName"  class="control-label">名称：</label>
		<div class="controls">
		<input type="text" id="taskName" name="taskName" maxlength="50" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group" style="display: none;" id="taskStatusCg">
		<label for="taskStatus"  class="control-label">状态：</label>
		<div class="controls">
		<input type="text" id="taskStatus" name="taskStatus" readonly="readonly" />
		</div>
	</div>
	<div class="control-group">
		<label for="taskProject"  class="control-label">工程名称：</label>
		<div class="controls">
		<input type="text" id="taskProject" name="taskProject" readonly="readonly" />
		<a href="javascript:;" class="btn" onclick="selectProject()">选择</a> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="taskExecPerson"  class="control-label">勘察人员：</label>
		<div class="controls">
		<input type="text" id="taskExecPerson" name="taskExecPerson" readonly="readonly" />
		<a href="javascript:;" class="btn" onclick="selectExecutePerson()">选择</a> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="taskTemplate"  class="control-label">模板：</label>
		<div class="controls">
		<input type="text" id="taskTemplate" name="taskTemplate" readonly="readonly" />
		<a href="javascript:;" class="btn" onclick="selectTemplate()">选择</a> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="taskPlanEndDate"  class="control-label">要求完成日期：</label>
		<div class="controls">
		<input type="text" id="taskPlanEndDate" name="taskPlanEndDate" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<!-- 
	<div class="control-group">
		<label for="taskPlanLongitude"  class="control-label">预设经度：</label>
		<div class="controls">
		<input type="text" id="taskPlanLongitude" name="taskPlanLongitude" />
		<a href="javascript:;" class="btn" onclick="selectPlanPoint()">选择</a> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="taskPlanLatitude"  class="control-label">预设纬度：</label>
		<div class="controls">
		<input type="text" id="taskPlanLatitude" name="taskPlanLatitude" />
		<a href="javascript:;" class="btn" onclick="selectPlanPoint()">选择</a> <span class="cms_color_required">*</span>
		</div>
	</div>
	 -->
	<div class="control-group">
		<label for="taskContent"  class="control-label">任务描述：</label>
		<div class="controls">
		<textarea id="taskContent" name="taskContent" maxlength="1000" ></textarea>
		</div> 
	</div>
	
	<!-- 新增字段  start -->
	<div class="control-group" style="display: none;" id="taskCreatorCg">
		<label for="taskCreator"  class="control-label">创建人员：</label>
		<div class="controls">
		<input type="text" id="taskCreator" name="taskCreator" readonly="readonly" />
		</div>
	</div>
	<!-- 新增字段  end -->
	
	<div class="control-group" style="display: none;" id="taskCreatorCg">
		<label for="taskCreator"  class="control-label">创建人员：</label>
		<div class="controls">
		<input type="text" id="taskCreator" name="taskCreator" readonly="readonly" />
		</div>
	</div>
	<div class="control-group" style="display: none;" id="taskCreateDateTimeCg">
		<label for="taskCreateDateTime"  class="control-label">创建时间：</label>
		<div class="controls">
		<input type="text" id="taskCreateDateTime" name="taskCreateDateTime" readonly="readonly" />
		</div>
	</div>
	<div class="form-actions">
		<a id="saveBtn" href="javascript:void(0);" class="btn btn-primary" onclick="doSave()"><i class="icon-ok"></i>保存</a>
		<a id="deleteBtn" href="javascript:void(0);" class="btn btn-danger" style="display: none;" onclick="doDelete()"><i class="icon-remove"></i>删除</a>
	</div>
</fieldset>
</form>